package com.example.dingzexi_homework1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.ViewHolder> {

    private Context context;
    private List<Friend> friendList;

    public FriendListAdapter(Context context, List<Friend> friendList) {
        this.context = context;
        this.friendList = friendList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_friend_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Friend friend = friendList.get(position);
        holder.friendImage.setImageResource(friend.getImageResId());
        holder.friendName.setText(friend.getName());
        holder.friendLastMessage.setText(friend.getLastMessage()); // 使用正确的变量名
    }
    @Override
    public int getItemCount() {
        return friendList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView friendImage;
        TextView friendName;
        TextView friendLastMessage; // 注意这里应该是friendLastMessage，不是friendLastMessage1

        ViewHolder(View view) {
            super(view);
            friendImage = view.findViewById(R.id.friend_image);
            friendName = view.findViewById(R.id.friend_name);
            friendLastMessage = view.findViewById(R.id.friend_last_message); // 使用正确的ID
        }
    }
}